/*
 * Copyright © 2021 the original author or authors.
 *
 * Licensed under the The MIT License (MIT) (the "License");
 *  You may obtain a copy of the License at
 *
 *         https://mit-license.org/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the “Software”), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.github.grossopa.selenium.examples.mat;

import com.github.grossopa.hamster.selenium.component.mat.main.MatAccordion;
import com.github.grossopa.hamster.selenium.component.mat.main.MatChipList;
import com.github.grossopa.hamster.selenium.component.mat.main.MatExpansionPanel;
import com.github.grossopa.selenium.core.component.WebComponent;
import com.github.grossopa.selenium.examples.helper.AbstractBrowserSupport;
import org.openqa.selenium.By;

import java.util.List;

import static com.github.grossopa.hamster.selenium.component.mat.MatComponents.mat;
import static com.github.grossopa.selenium.core.driver.WebDriverType.EDGE;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Tests the actual features of {@link MatChipList}.
 *
 * @author Jack Yin
 * @since 1.6
 */
public class MatExpansionPanelTestCases extends AbstractBrowserSupport {

    public void testExpansionPanel() {
        driver.navigate().to("https://material.angular.io/components/expansion/examples");

        WebComponent container = driver.findComponent(By.id("expansion-expand-collapse-all"));
        MatAccordion accordion = container.findComponent(By.tagName("mat-accordion")).as(mat()).toAccordion();

        assertTrue(accordion.validate());

        List<MatExpansionPanel> expansionPanels = accordion.getExpansionPanels();
        assertEquals(3, expansionPanels.size());
        assertTrue(expansionPanels.stream().allMatch(MatExpansionPanel::validate));
        assertTrue(expansionPanels.stream().noneMatch(MatExpansionPanel::isExpanded));

        assertTrue(expansionPanels.get(0).isEnabled());
        assertFalse(expansionPanels.get(1).isEnabled());
        assertTrue(expansionPanels.get(2).isEnabled());

        expansionPanels.get(0).expand();
        // waiting for animation to end
        driver.threadSleep(500L);
        assertTrue(expansionPanels.get(0).isExpanded());

        assertEquals("Personal data",
                expansionPanels.get(0).findComponent(By.className("mat-expansion-panel-header-title")).getText());

        assertEquals(2, expansionPanels.get(0).getExpansionPanelBody().findComponents(By.tagName("input")).size());

        expansionPanels.get(0).collapse();
        // waiting for animation to end
        driver.threadSleep(500L);
        assertFalse(expansionPanels.get(0).isExpanded());
    }

    public static void main(String[] args) {
        MatExpansionPanelTestCases test = new MatExpansionPanelTestCases();
        try {
            test.setUpDriver(EDGE);
            test.testExpansionPanel();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            throw ex;
        }
    }
}
